import Vue from "vue";
import VueRouter from "vue-router";
import Login from "../views/login";
import Home from "../views/home";
import Demoestic from "../components/demoestic/demoestic";
import ShoppingCart from "../components/shoppingCart/shoppingCart";
import TravelRequest from "../components/travelRequest/travelRequest";
import RequestConfirm from "../components/travelRequest/requestConfirm";
import Payment from "../components/travelRequest/payment";
Vue.use(VueRouter);
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err);
};
const router = new VueRouter({
  routes: [
    {
      path: "/",
      redirect: "/login"
    },
    {
      name: "login",
      path: "/login",
      component: Login,
      meta: {
        title: "登录"
      }
    },
    {
      name: "HOME",
      path: "/home",
      component: Home,
      redirect: "/demoestic",
      children: [
        {
          path: "/demoestic",
          component: Demoestic,
          meta: {
            title: "国内航班/Demoestic Air"
          }
        },
        {
          path: "/shoppingcart",
          component: ShoppingCart,
          meta: {
            title: "购物车"
          }
        },
        {
          name: "request",
          path: "/request",
          component: TravelRequest,
          meta: {
            title: "差旅申请单"
          }
        },
        {
          name: "requestconfirm",
          path: "/requestconfirm",
          component: RequestConfirm,
          meta: {
            title: "差旅申请单确认订单"
          }
        },
        {
          name: "payment",
          path: "/payment",
          component: Payment,
          meta: {
            title: "付款界面"
          }
        },
      ]
    }
  ]
});

router.beforeEach((to, from, next) => {
  //beforeEach是router的钩子函数，在进入路由前执行

  if (to.meta.title) {
    //判断是否有标题

    document.title = to.meta.title;
  }

  next(); //执行进入路由，如果不写就不会进入目标页
});
export default router;
